package com.hushui.mall.commodity.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hushui.common.utils.BeanUtilCopy;
import com.hushui.mall.commodity.entity.vo.CategoryVo;
import com.hushui.mall.commodity.entity.vo.UpdateCategoryVo;

import com.hushui.mall.commodity.vo.Catalog2Vo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hushui.mall.commodity.entity.CategoryEntity;
import com.hushui.mall.commodity.service.CategoryService;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;



/**
 * 商品三级分类
 *
 * @author miya
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:38:44
 */
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("commodity/category/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatelogJson(){
        Map<String, List<Catalog2Vo>> map = categoryService.getCatelogJson();
        return map;
    }

    @PostMapping("/product/category/update/sort")
    public R updateCategory(@RequestBody List<UpdateCategoryVo> list){
        ArrayList<CategoryEntity> categoryEntities = new ArrayList<>();
        BeanUtils.copyProperties(list,categoryEntities);
        for (CategoryEntity a:categoryEntities) {
            if (a.getParentCid() != -1L && a.getCatLevel() != -1 && a.getSort() == -1) {
                QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("cat_id", a.getCatId());
                CategoryEntity categoryEntity = new CategoryEntity();
                categoryEntity.setParentCid(a.getParentCid());
                categoryEntity.setCatLevel(a.getCatLevel());
                categoryService.update(categoryEntity, wrapper);
            }else if (a.getParentCid() != -1L && a.getCatLevel() == -1 && a.getSort() != -1) {
                QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("cat_id", a.getCatId());
                CategoryEntity categoryEntity = new CategoryEntity();
                categoryEntity.setParentCid(a.getParentCid());
                categoryEntity.setSort(a.getSort());
                categoryService.update(categoryEntity, wrapper);
            }else if (a.getParentCid() == -1L && a.getCatLevel() != -1 && a.getSort() != -1) {
                QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("cat_id", a.getCatId());
                CategoryEntity categoryEntity = new CategoryEntity();
                categoryEntity.setCatLevel(a.getCatLevel());
                categoryEntity.setSort(a.getSort());
                categoryService.update(categoryEntity, wrapper);
            }else if (a.getParentCid() != -1L && a.getCatLevel() == -1 && a.getSort() == -1) {
                QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("cat_id", a.getCatId());
                CategoryEntity categoryEntity = new CategoryEntity();
                categoryEntity.setParentCid(a.getParentCid());
                categoryService.update(categoryEntity, wrapper);
            }else if (a.getParentCid() == -1L && a.getCatLevel() != -1 && a.getSort() == -1) {
                QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("cat_id", a.getCatId());
                CategoryEntity categoryEntity = new CategoryEntity();
                categoryEntity.setCatLevel(a.getCatLevel());
                categoryService.update(categoryEntity, wrapper);
            }else if (a.getParentCid() == -1L && a.getCatLevel() == -1 && a.getSort() != -1) {
                QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("cat_id", a.getCatId());
                CategoryEntity categoryEntity = new CategoryEntity();
                categoryEntity.setSort(a.getSort());
                categoryService.update(categoryEntity, wrapper);
            }else if (a.getParentCid() != -1L && a.getCatLevel() != -1 && a.getSort() != -1) {
                QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("cat_id", a.getCatId());
                CategoryEntity categoryEntity = new CategoryEntity();
                categoryEntity.setParentCid(a.getParentCid());
                categoryEntity.setCatLevel(a.getCatLevel());
                categoryEntity.setSort(a.getSort());
                categoryService.update(categoryEntity, wrapper);
            }
        }

        return R.ok();
    }

    @GetMapping("/product/category/list/tree")
    public R categoryList(){
        List<CategoryEntity> list = categoryService.list();
        List<CategoryVo> categoryVos = BeanUtilCopy.copyListProperties(list, CategoryVo::new);
        for (CategoryVo a:categoryVos
             ) {
            QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_cid",a.getCatId());
            List<CategoryEntity> list1 = categoryService.list(wrapper);
            List<CategoryVo> categoryVos1 = BeanUtilCopy.copyListProperties(list, CategoryVo::new);
        }
        return R.ok().put("data",list);

    }

    /**
     * 信息
     */
    @RequestMapping("commodity/category/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);
        
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("commodity/category/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("commodity/category/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("commodity/category/delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
